package com.easywan.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.easywan.core.HttpRequest;
import com.easywan.service.IBendibaoService;
import com.easywan.util.StringsUtils;
import com.easywan.util.Utils;
import com.easywan.vo.Bendibao;
import com.easywan.vo.Pages;
@Service
public class BendibaoServiceImpl implements IBendibaoService {
	
	@Override
	public List<Bendibao> list(Pages pages) {
		List<Bendibao> result = new ArrayList<>();
		String activeListUrl = "http://ly.sz.bendibao.com/tour/8972_[1].html".replace("[1]", pages.getPage()+"");
		String html = com.easywan.core.HttpRequest.get(activeListUrl).body();
		String listHtml = StringsUtils.findMid(html, "<div class=\"content\">","<div class=\"Pagination\">");
		//System.out.println(listHtml);
		
		String regexTitle = "<a[^_]*_blank\">(.+?)</a>";
		List<String> titles = StringsUtils.mathesAndNot(listHtml, regexTitle, "img");
		List<String> sources = StringsUtils.mathesAnd(listHtml, "<a href=\"(.+?)\"[^_]*_blank\">", "http://ly.sz.bendibao.com/");
		//System.out.println(titles);
		String regexTime = "<dd class=\"icon_zise\">(.+?)</dd>";
		List<String> times = StringsUtils.mathesAnd(listHtml, regexTime, "时间");
		//System.out.println("时间:"+times);
		List<String> addrs = StringsUtils.mathesAnd(listHtml, regexTime, "地点");
		//System.out.println(addrs);
		List<String> piaos = StringsUtils.mathesAnd(listHtml, regexTime, "票价");
		//System.out.println(piaos);
		String regexCover = "src=\"(.+?)\"";
		List<String> covers = StringsUtils.mathes(listHtml,regexCover);
		//System.out.println(covers);
		for(int i = 0; i < titles.size();i++){
			/*System.out.println("title:"+titles.get(i));
			System.out.println("time:"+times.get(i));
			System.out.println("address:"+addrs.get(i));
			System.out.println("piao:"+piaos.get(i));
			System.out.println("coverHref:"+covers.get(i));*/
			Bendibao b = new Bendibao();
			b.setTitle(titles.get(i));
			b.setTime(times.get(i));
			b.setAddress(addrs.get(i));
			b.setPiao(piaos.get(i));
			b.setCoverHref(covers.get(i));
			b.setSource(sources.get(i));
			result.add(b);
		}
		
		return result;
	}

	@Override
	public String get(Bendibao bendibao) {
		String source = bendibao.getSource();
		HttpRequest.setCharset("GB2312");
		String html = HttpRequest.get(source).body();
		String content = StringsUtils.findMid(html, "<!--文章正文区开始-->", "<!--文章正文区结束-->");
		List<String> pages = StringsUtils.mathes(html, "<span>共<b>(.+?)</b>页</span>");
		if(!Utils.listIsEmpty(pages)){
			int page = Integer.valueOf(pages.get(0));
			for(int i = 2; i <= page; i++){
				String sourceTemp = source.replace(".html", "_"+i+".html");
				System.out.println(sourceTemp);
				String pageHtml = HttpRequest.get(sourceTemp).body();
				content += StringsUtils.findMid(pageHtml, "<!--文章正文区开始-->", "<!--文章正文区结束-->");
			}
		}
		HttpRequest.setCharset("UTF-8");
		return content;
	}
	
}
